<!--商品规格-->
<div ng-init="checkobj(pagedata.item,'{name}');checkarray(pagedata.item.{name},'specs');"></div>
<div class="spec_item" ng-repeat="spec in pagedata.item.{name}.specs">
		<div class="form-group">
			<label class="col-xs-12 col-sm-3 col-md-2 right-label">规格名称</label>
			<div class="col-xs-12 col-sm-8 col-lg-9">
				<div class="input-group">
					<input type="text" class="form-control  spec_title" ng-model="spec.title" placeholder="规格名称 (比如: 颜色)"/>
					<div class="input-group-btn">
						<a href="javascript:;" ng-click="new_option={};spec.options.push(new_option)" class='btn btn-info add-specitem'><i class="fa fa-plus"></i> 添加规格项</a>
						<a href="javascript:;" class="btn btn-danger" ng-click="pagedata.item.{name}.specs.splice($index,1)"><i class="fa fa-remove"></i></a>
					</div>
				</div>
			</div>
		</div>
		<div class="form-group">
			<label class="col-xs-12 col-sm-3 col-md-2 right-label">规格详情</label>
			<div class="col-xs-12 col-sm-8 col-lg-9">
				<div class="spec_item_items">
					<div ng-repeat="option in spec.options" class="spec_item_item" style="float:left;margin:5px;width:250px; position: relative">
						<div class="input-group">
							<input type="text" class="form-control spec_item_title" ng-model="option.title"/>
							<span class="input-group-addon">
								<a href="javascript:;" ng-click="spec.options.splice($index,1)" title='删除'><i class="fa fa-times"></i></a>
							</span>
						</div>
					</div>
				</div>
			</div>
		</div> 
</div>
<div class="form-group">
	<a href="javascript:;" class="btn btn-primary" ng-click="new_spec={'title':'','options':[]};pagedata.item.{name}.specs.push(new_spec);" style="margin-top:10px;margin-bottom:10px;" title="添加规格"><i class='fa fa-plus'></i> 添加规格</a> 
	<a href="javascript:;" ng-click="pagedata.item.{name}.tds=func.spec_made()" title="刷新规格项目表" class="btn btn-primary"><i class="fa fa-refresh"></i> 刷新规格项目表</a>
</div>
<div class="clearfix table-responsive" ng-init="pagedata.default_values={}" ng-if="pagedata.item.{name}.tds">
	<table class="table table-bordered table-condensed jinyun-table">
		<thead class="navbar-inner">
			<tr class="active">
				<th width="80px" style="text-align:center;" ng-repeat="spec in pagedata.item.{name}.specs">{{spec.title}}</th>
				<th style="text-align:center;" ng-repeat="field in {fields}">
					<div style="text-align:center;">{{field.title}}</div>
					<div class="input-group" >
					<img ng-if="field.type=='image'" ng-click="image(pagedata.default_values,field.name)" ng-src="{{tomedia(pagedata.default_values[field.name]) || '../core/resource/images/nopic-small.jpg'}}" style="width:30px;height:30px;padding:1px;border:1px solid #ccc;"/>
					<input ng-if="field.type=='string'" class="form-control option_stock option valid" type="text" ng-model="pagedata.default_values[field.name]"/>
					<span class="input-group-addon" ng-click="func.spec_set(field.name,pagedata.default_values[field.name])">
						<i class="fa fa-angle-double-down" title="批量设置"></i>
					</span>
					</div>
				</th>
				
			</tr>
		</thead>
			<tr ng-repeat="(index,td) in pagedata.item.{name}.tds">
				<td class="text-left full" style="text-align:center;"ng-repeat="(key,spec) in pagedata.item.{name}.specs" ng-if="td[spec.title]!=pagedata.item.{name}.tds[index-1][spec.title]" rowspan="{{pagedata.item.{name}.specs[key+1].options.length || 1}}">
					{{td[spec.title]}}
				</td>
				<td ng-repeat="field in {fields}">
					<img ng-if="field.type=='image'" ng-click="image(td,field.name)" ng-src="{{tomedia(td[field.name]) || '../core/resource/images/nopic-small.jpg'}}" style="width:30px;height:30px;padding:1px;border:1px solid #ccc;"/>
					<input ng-if="field.type=='string'" class="form-control option_stock option valid" type="text" ng-model="td[field.name]"/>
				</td>
				
			</tr>
	</table>
</div>
<script>
window.scope.func.spec_set=function(key,value){
	$.each(window.scope.pagedata.item.{name}.tds,function(index,td){
		td[key]=value;
	});
}
window.scope.func.spec_made=function(){
	//刷新规格项目表，如果只是增加规格项，则保留原有规格商品，如何保留呢。两个问题：如何判断是仅改规格项？两个规格数据库都存有。如何保留原有规格商品？生成tds后，遍历原tds，然后进行赋值
	var isnew=false;
	$.each(scope.pagedata.item.{name}.specs,function(i,spec){
		if(!spec.id){
			isnew=true;
		}
	});
	if(!scope.pagedata.item.{name}.tds || scope.pagedata.item.{name}.tds.length<=0){
		isnew=true;
	}
	
	var tds=[{'num':0}];
	$.each(window.scope.pagedata.item.{name}.specs,function(i,spec){
		var new_tds=[];
		$.each(tds,function(j,k){
			$.each(spec.options,function(l,m){
				var new_td=angular.copy(k);
				new_td[spec.title]=m.title;
				new_tds.push(new_td);
			});
			
		});
		tds=new_tds;
	});
	if(!isnew){
		$.each(tds,function(j,k){
			$.each(scope.pagedata.item.{name}.tds,function(j_old,k_old){
				var like=true;
				$.each(scope.pagedata.item.{name}.specs,function(i,spec){
					if(k[spec.title]!=k_old[spec.title]){
						like=false;
					}
				});
				if(like){
					tds[j]=angular.copy(k_old);;
				}
			});
		})
	}
	
	//console.log(tds);
	return tds;
}
</script>